package com.pro.addresslist.ui.common;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.BadTokenException;
import android.widget.ImageView;
import android.widget.TextView;

import com.pro.addresslist.R;

/**
 * lkh增加mapp接口。20161215
 * @author guanguan.li
 * 
 */
public abstract class BaseActivity extends FragmentActivity implements
OnClickListener{

	// Title的控器
	private ImageView mIvTitleLeft, mIvTitleRight;
	private TextView  mTvTitleRight, mTvTitleCenter,mTvTitleLeft;

	public static Context mContext;
	private ProgressDialog progressDialog;
	private boolean loadTitle = false;
	/** 全局的LayoutInflater对象，已经完成初始化. */
	public LayoutInflater mInflater;
	private WindowManager.LayoutParams lp;

	public MyApplication mapp;//增加mapp接口
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		mInflater = LayoutInflater.from(this);
		MyApplication.getInstance().addActivity(this);
		mapp = (MyApplication)getApplication();
		if (getLayout() != 0) {
			setContentView(getLayout());
		}
		mContext = this;
		if ((findViewById(R.id.rl_title_layout)) != null) {
			loadTitle();
			loadTitle = true;
		}
		OnActCreate(savedInstanceState);
	}

	/**
	 * 加载标题
	 */
	private void loadTitle() {
		mIvTitleLeft = (ImageView) findViewById(R.id.iv_title_left_image);
		mIvTitleRight = (ImageView) findViewById(R.id.iv_title_right_image);
		mTvTitleCenter = (TextView) findViewById(R.id.tv_title_center_text);
		mTvTitleRight = (TextView) findViewById(R.id.tv_title_right_text);
		mTvTitleLeft = (TextView) findViewById(R.id.tv_title_left_text);
	}


	// 设置左边图片 自定义点击事件
	public void setLeftIamge(int resId, OnClickListener listener) {
		if (loadTitle) {
			mIvTitleLeft.setVisibility(View.VISIBLE);
			mIvTitleLeft.setImageResource(resId);
			mIvTitleLeft.setOnClickListener(listener);

		}
	}
	//设置左边字体
	public  void setLeftText(String text){
		if (loadTitle) {
			mTvTitleLeft.setVisibility(View.VISIBLE);
			mTvTitleLeft.setText(text);
		}
	}
	
	public void setLeftHide(){
		mTvTitleLeft.setVisibility(View.GONE);
	}
	// 设置左边图片 默认返回
	public void setLeftImageBack() {
		if (loadTitle) {
			mIvTitleLeft.setVisibility(View.VISIBLE);
			mIvTitleLeft.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					finish();
				}
			});
		}

	}
	public void showLeftImage(){
		if (loadTitle) {
			mIvTitleLeft.setVisibility(View.VISIBLE);

		}
	}
	
	//隐藏左边图片
		public void setLeftImageHide() {
			mIvTitleLeft.setVisibility(View.GONE);
		}
//		 设置左边字体 自定义点击事件
		public void setLeftText(String text, OnClickListener listener) {
			if (loadTitle) {
				mTvTitleLeft.setVisibility(View.VISIBLE);
				mTvTitleLeft.setText(text);
				mTvTitleLeft.setOnClickListener(listener);
			}
		}

	// 设置标题
		public void setTtile(String text) {
			if (loadTitle) {
				mTvTitleCenter.setVisibility(View.VISIBLE);
				mTvTitleCenter.setText(text);
			}
		}

	// 设置右边图片 自定义点击事件
	public void setRightImage(int resid, OnClickListener listener) {
		if (loadTitle) {
			mIvTitleRight.setVisibility(View.VISIBLE);
			mIvTitleRight.setImageResource(resid);
			mIvTitleRight.setOnClickListener(listener);
		}
	}

	// 设置右边字体 自定义点击事件
	public void setRightText(String text, OnClickListener listener) {
		if (loadTitle) {
			mTvTitleRight.setVisibility(View.VISIBLE);
			mTvTitleRight.setText(text);
			mTvTitleRight.setOnClickListener(listener);
		}
	}
	
	// 设置右边字体 自定义点击事件
	public void setRightText(String text) {
		if (loadTitle) {
			mTvTitleRight.setVisibility(View.VISIBLE);
			mTvTitleRight.setText(text);
		}
	}
	public void setRightHide(){
		mTvTitleRight.setVisibility(View.GONE);
	}
	/**
	 * 给控件设置监听
	 * 
	 * @param resId
	 * @param listener
	 */
	public View setViewClick(int resId, OnClickListener listener) {
		View view = findViewById(resId);
		if (view != null) {
			view.setOnClickListener(listener);
		}
		return view;
	}

	/**
	 * 给控件设置监听
	 * 
	 * @param resId
	 * @param listener
	 */
	public View setViewClick(int resId) {
		return setViewClick(resId, this);
	}

	@Override
	public void onClick(View v) {
		OnViewClick(v);
	}

	

	/**
	 * 显示短时间的提示
	 * 
	 * @param info
	 */
	public void ShowToast(String info) {
		MyApplication.getInstance().showToast(info);
	}

	/**
	 * 显示正在加载的进度条
	 * 
	 */
	public void showProgressDialog() {
		showProgressDialog("努力加载中...");
	}
	public void showProgressDialog(String msg) {
		if (progressDialog != null && progressDialog.isShowing()) {
			progressDialog.dismiss();
			progressDialog = null;
		}
		progressDialog = new ProgressDialog(this);
		progressDialog.setMessage(msg);
		progressDialog.setCanceledOnTouchOutside(false);
		progressDialog.setCancelable(true);
		try {
			progressDialog.show();
		} catch (BadTokenException exception) {
			exception.printStackTrace();
		}
	}

	//	public void showProgressingDialog() {
	//		if (progressDialog != null && progressDialog.isShowing()) {
	//			progressDialog.dismiss();
	//			progressDialog = null;
	//		}
	//		progressDialog = new ProgressDialog(this);
	//		progressDialog.setMessage("努力加载�?...");
	//		progressDialog.setCanceledOnTouchOutside(false);
	//		progressDialog.setCancelable(true);
	//		try {
	//			progressDialog.show();
	//		} catch (BadTokenException exception) {
	//			exception.printStackTrace();
	//		}
	//	}

	/**
	 * 取消对话框显示
	 */
	public void disMissDialog() {
		try {
			if (progressDialog != null && progressDialog.isShowing()) {
				progressDialog.dismiss();
				progressDialog = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 判断
	 * 
	 * @param string
	 * @return
	 */
	public String isEmpty(String string) {
		return TextUtils.isEmpty(string) ? " " : string;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		MyApplication.getInstance().unDestroyActivityList.remove(this);
	
	}


	/**
	 * 获取界面要加载的布局文件
	 * 
	 * @return
	 */
	public abstract int getLayout();

	/**
	 * 子类的OnCreate()方法
	 * 
	 * @param savedInstanceState
	 */
	public abstract void OnActCreate(Bundle savedInstanceState);

	/**
	 * 控件的点击事�?
	 * 
	 * @param v
	 */
	public abstract void OnViewClick(View v);

	
	/**
	 * 获取Logo（暂用于分享获取logo）
	 * 
	 * @return
	 */
	public String getLogo() {
		try {
			InputStream is = getResources().getAssets().open("ic_launcher.png");
			File file = new File(Environment.getExternalStorageDirectory(),"ic_launcher.png");
			FileOutputStream fos = new FileOutputStream(file);
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = is.read(buffer)) != -1) {
				fos.write(buffer, 0, len);
			}
			fos.flush();
			fos.close();
			is.close();
			return Environment.getExternalStorageDirectory() + "/ic_launcher.png";
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 屏幕变暗
	 */
	public void showScreenDark() {
		lp = getWindow().getAttributes();
		lp.alpha = 0.4f;
		getWindow().setAttributes(lp);
	}

	/**
	 * 屏幕变亮
	 */
	public void showScreenLight() {
		lp = getWindow().getAttributes();
		lp.alpha = 1f;
		getWindow().setAttributes(lp);
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}
}
